/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import Model.Customer;
import Model.Orderproducts;
import Model.Orders;
import Model.Paymenttype;
import Model.Products;
import Model.User;
import java.util.List;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.persistence.EntityManager;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.servlet.http.HttpServletRequest;
import org.primefaces.event.FlowEvent;

/**
 *
 * @author gulben
 */
@ManagedBean (name = "basketController")
@RequestScoped
public class BasketController {

    public Customer customer;
    private boolean skip;
    private Orders order = new Orders();
    
    public BasketController() {
    }
    
    
    public String getName() {
        getUser();
        return customer.getName();
    }
    
    public String getSurname() {
        getUser();
        return customer.getSurname();
    }
    
    public String getEmail() {
        getUser();
        return customer.getEmail();
    }
    
    public String getAddress() {
        getUser();
        return customer.getAddress();
    }
    
    public int getOrderId() {
        return order.getId();
    }
    
    public void getUser() {
        Map<String, Object> session = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
        User user = (User) session.get("user");
        String username = (String) user.getUsername();
        EntityManager entityManager =  Persistence.createEntityManagerFactory("KitapcdappPU").createEntityManager();
        Query query = entityManager.createNamedQuery("Customer.findByCustomerUsername");
        query.setParameter("username", username);
        List<Customer> customerList = (List<Customer>)query.getResultList();
        customer = customerList.get(0);
    }
    
    public List<Paymenttype> getPaymentType() {
        EntityManager entityManager =  Persistence.createEntityManagerFactory("KitapcdappPU").createEntityManager();
        Query query = entityManager.createNamedQuery("Paymenttype.findAll");
        List<Paymenttype> paymentTypes = (List<Paymenttype>)query.getResultList();
        return paymentTypes;
    }
    
    public Products getProduct() {
        EntityManager entityManager =  Persistence.createEntityManagerFactory("KitapcdappPU").createEntityManager();
        Query query = entityManager.createNamedQuery("Products.findById");
        query.setParameter("id", Integer.parseInt(getParametresForProduct()));
        List<Products> products = (List<Products>)query.getResultList();
        return products.get(0);
    }

    public String getParametresForProduct() {
        Object req = FacesContext.getCurrentInstance().getExternalContext().getRequest();
        String productId = ((HttpServletRequest) req).getParameter("productId");
        return productId;
    }
    
    
    public boolean isSkip() {
        return skip;
    }
 
    public void setSkip(boolean skip) {
        this.skip = skip;
    }
    
    public String onFlowProcess(FlowEvent event) {
        if(skip) {
            skip = false;   //reset in case user goes back
            return "confirm";
        }
        else {
            return event.getNewStep();
        }
    }
}
